#include  "DRV_Timer.h"
#include "DRV_WS2812B.h"
unsigned int DataCount=0;
void Timer_Init_TM0(void)
{
    /* TIMER0 configuration: generate PWM signals with different duty cycles:
       TIMER0CLK = SystemCoreClock / 108 = 1MHz */
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER0);
    timer_deinit(TIMER0);

    /* TIMER0 configuration */
    timer_initpara.prescaler         = 53;//--1MHz
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 1;//-1mS--
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER0,&timer_initpara);

     /* CH0 configuration in PWM mode */
//    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
//    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
//    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
//    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
//    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
//    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
//    timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);

//    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,250);
//    timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
//    timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

    timer_primary_output_config(TIMER0,ENABLE);
    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER0);
    timer_enable(TIMER0);
    nvic_irq_enable(TIMER0_UP_IRQn,1,1);        //--
    timer_interrupt_enable(TIMER0,TIMER_INT_UP);    //--
}

unsigned int Data=0;
void TIMER0_UP_IRQHandler(void)
{
	timer_interrupt_flag_clear( TIMER0 ,TIMER_INT_FLAG_UP);

	if(Data<WS2812B_Bit[DataCount])
	{
        gpio_bit_set(GPIOB, GPIO_PIN_15);
	}
	if(Data>WS2812B_Bit[DataCount])
	{
	  gpio_bit_reset(GPIOB, GPIO_PIN_15);
	}
	Data++;
	if(Data>=30)    Data=0;
		
	DataCount++;

	if(DataCount >=((unsigned int) WS2812B_NumBit))
	{
        DataCount=0;
	}

}
	
